package com.gem.day16;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**服务端*/
public class ServerTCP {
	public static void main(String[] args) throws IOException {
		System.out.println("服务器启动...");
		//创建服务端套接字
		ServerSocket serverSocket = new ServerSocket(9999);
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						//建立连接
						Socket socket = serverSocket.accept();
						//创建流对象
						BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
						BufferedOutputStream bos = new BufferedOutputStream(
								new FileOutputStream("E:\\upload\\" + System.currentTimeMillis() + ".pdf"));
						//读写数据
						byte[] buf = new byte[10 * 1024];
						int len = -1;
						while ((len = bis.read(buf)) != -1) {
							bos.write(buf, 0, len);
						}
						bos.close();
						bis.close();
						socket.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
}
